package com.dhecp.project.common.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.log.BeanLogUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.domain.UpdateLog;
import com.dhecp.project.common.mapper.UpdateLogMapper;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 数据变更记录表
 * @author: hlf
 * @create: 2022-09-09 10:45
 */
@Service
public class UpdateLogServiceImpl extends BaseModuleServiceImpl implements UpdateLogService {

    @Autowired
    private UpdateLogMapper updateLogMapper;
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    @Override
    public UpdateLog findByGuid(Map<String, Object> params) throws Exception {
        return updateLogMapper.findById(params);
    }


    @Override
    public List<UpdateLog> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        PageUtils.startPage();
        return updateLogMapper.select(params);
    }


    /**
     *@Description: 添加修改日志
     *@Param: [moduleTable] 字典标识
     *@Param: [targetGuid] 数据标识
     *@Param: [oldBean] 旧对象
     *@Param: [newBean] 新对象
     *@Param: [operatorName] 操作名称
     *@return: com.dhecp.common.core.domain.AjaxResult
     *@Author: hlf
     *@date: 2022-09-13 14:39
     */;
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult addUpdateLog(String moduleTable,String targetGuid,Object oldBean, Object newBean, String operatorName) {

        try {
            UpdateLog updateLog = new UpdateLog();
            String AlterTheContent = BeanLogUtils.getChangedFields(oldBean,newBean);
            updateLog.setGuid(RandomValueUtils.getGUID());
            updateLog.setUserId(ShiroUtils.getUserId());
            updateLog.setCreateTime(new Date());
            updateLog.setActionType(operatorName);
            updateLog.setModuleTable(moduleTable);
            updateLog.setModuleGuid(targetGuid);
            updateLog.setAlterTheContent(AlterTheContent);
            if (AlterTheContent.length() > 0){
                updateLogMapper.insert(updateLog);
            }
        }catch (Exception e) {
           return AjaxResult.error(String.valueOf(e));
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    /**
     *@Description: 添加 新增/删除 日志
     *@Param: [moduleTable] 字典标识
     *@Param: [targetGuid] 数据标识
     *@Param: [oldBean] 旧对象
     *@Param: [newBean] 新对象
     *@Param: [operatorName] 操作名称
     *@return: com.dhecp.common.core.domain.AjaxResult
     *@Author: hlf
     *@date: 2022-09-13 14:39
     */;
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult addInsertLog(String moduleTable,String targetGuid, String operatorName) {

        try {
            UpdateLog updateLog = new UpdateLog();
            updateLog.setGuid(RandomValueUtils.getGUID());
            updateLog.setUserId(ShiroUtils.getUserId());
            updateLog.setCreateTime(new Date());
            updateLog.setActionType(operatorName);
            updateLog.setModuleTable(moduleTable);
            updateLog.setModuleGuid(targetGuid);
            updateLogMapper.insert(updateLog);
        }catch (Exception e) {
           return AjaxResult.error(String.valueOf(e));
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
    
}